package com.example.wc;

/**
 *
 *  * @projectName myflinkstu
 *  * @title     WordCount
 *  * @package    com.example.wc
 *  * @description
 *  * @author hjx
 *  * @date   2022-3-17 14:28
 *  * @version V1.0.0
 *  * @copyright 2022 ty
 *
 */
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.util.Collector;

// 批处理word count
public class WordCount {
    // 自定义类，实现FlatMapFunction接口
    public static class MyFlatMapper implements FlatMapFunction<String, Tuple2<String, Integer>> {
        public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
            // 按空格分词
            String[] words = value.split(" ");
            // 遍历所有word，包成二元组输出
            for (String word : words) {
                out.collect(new Tuple2(word, 1));
            }
        }
    }
}
